﻿// File: CsoGateway.Collections.Stack.js
// Version: 0.7.1.0
// Author: Pascal Dufresne
// Date: 2009-12-06
// Last update: 2009-05-25
// http://csogateway.codeplex.com
// http://csogateway.metaobjects.ca
// Copyright (C) 2010 Pascal Dufresne

/*
 * Register CsoGateway.Collections namespace. Import CsoGateway.System namespace.
 */
Type.registerNamespace("CsoGateway.Collections");
ImportNamespace(CsoGateway.System);

/*
 * CsoGateway.Collections.Stack implements the common stack abstract data type.
 * It's interface is meant to resemble that of the .NET class
 * System.Collections.Generic.Stack<System.Object>
 * It's internal implementation is a CsoGateway.Collections.List.
 *
 * It allows the same object to be pushed in many times.
 * Null values can be added but not undefined values.
 *
 *
 * Contructor                       'System.Collections.Generic.Stack<System.Object>' equivalent
 * ----------                       -----------------------------------------------------------
 * Stack()                          Stack()
 * Stack(<argument list>)           Stack(System.Collections.Generic.IEnumerable<System.Object>)
 *
 *
 * Methods                          'System.Collections.Generic.List<System.Object>' equivalent
 * ----------                       -----------------------------------------------------------
 * Count()                          Count()
 * Clear()                          Clear()
 * Contains(Object)                 Contains(System.Object)
 * Peek()                           Peek()
 * Pop()                            Pop()
 * Push(Object)                     Push(System.Object)
 * Count()                          Count(System.Object)
 * IsEmpty()                        -
 *
 */

ImportType(CsoGateway.Collections.List);

/*
 * Creates and instance of CsoGateway.Collections.Stack.
 * There are 2 ways to create a Stack object:
 * - Stack()
 *		Creates and empty Stack.
 *		Ref: System.Collection.Generic.Stack.Stack()
 *
 * - Stack(object1, object2, object3, ...)
 *		Creates a Stack and pushes the objects in the parameter list in the Stack. The last object
 *		to pop out of the Stack will be the first in the paremeter list.
 *		Ref: System.Collection.Generic.Stack.Stack(System.Collections.Generic.IEnumerable<System.Object>)
 */

CsoGateway.Collections.Stack = function Stack()
{	
	this.innerList = new List();
	for(var i=0; i<arguments.length; ++i)
	{
		this.Push(arguments[i]);
	}
}

/*
 * Gets the number of elements contained in the Stack.
 * Ref: System.Collection.Generic.Stack.Count 
 */
CsoGateway.Collections.Stack.prototype.Count =  function()
{
	return (this.innerList.Count());
}

/*
 * Removes all items from the Stack.
 * Ref: System.Collection.Generic.Stack.Clear
 */
CsoGateway.Collections.Stack.prototype.Clear = function()
{	
	this.innerList.Clear();
}

/*
 * Determines whether the Stack contains a specific object.
 * The element can be null but cannot be undefined or an exception is thrown.
 * Ref: System.Collection.Generic.Stack.Contains
 */
CsoGateway.Collections.Stack.prototype.Contains = function(element)
{	
	return this.innerList.Contains(element);
}

/*
 * Returns the object at the top of the Stack without removing it.
 * Ref: System.Collection.Generic.Stack.Peek
 */
CsoGateway.Collections.Stack.prototype.Peek = function()
{
	if(this.innerList.Count() == 0)
		throw new Error('CsoGateway.Collections.Stack.Peek: The stack is empty.');
	
	return this.innerList.Last();
}

/*
 * Removes and returns the object at the top of the Stack.
 * Ref: System.Collection.Generic.Stack.Pop
 */
CsoGateway.Collections.Stack.prototype.Pop = function()
{	
	if(this.innerList.Count() == 0)
		throw new Error('CsoGateway.Collections.Stack.Pop: The stack is empty.');
	return this.innerList.RemoveAt(this.innerList.Count()-1);
}

/*
 * Inserts an object at the top of the Stack. 
 * The element can be null but cannot be undefined or an exception is thrown.
 * Ref: System.Collection.Generic.Stack.Push
 */
CsoGateway.Collections.Stack.prototype.Push = function(element)
{	
	return this.innerList.Add(element);
}

/*
 * Tests whether this stack is currently empty (has no elements).
 */
CsoGateway.Collections.Stack.prototype.IsEmpty =  function()
{
	return (this.innerList.IsEmpty());
}

CsoGateway.Collections.Stack.registerClass('CsoGateway.Collections.Stack', CsoNative, Sys.IDisposable);

/*
 * End of CsoGateway.Collections.Stack definition
 */

